home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13905 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Path: csun.edu!kc44097
  2. From: kc44097@csun.edu (chen)
  3. Newsgroups: comp.lang.c
  4. Subject: Union type
  5. Date: 10 Apr 1996 20:01:51 GMT
  6. Organization: California State University, Northridge
  7. Message-ID: <4kh43f$h4f@dewey.csun.edu>
  8. NNTP-Posting-Host: louie.csun.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12.      I have a question about union,can anyonegive me some advice?
  13. Please e-mail me
  14.                         kc44097@huey.csun.edu
  15.                  Thankx
  16. -------------------------------------------------------------
  17.  
  18. #include <stdio.h>
  19.  
  20. union {
  21.   int   integer;
  22.   float floating;
  23. } myUnion;
  24.  
  25.  
  26. main()
  27. {
  28.  
  29.   myUnion.integer = 1;
  30.  
  31.    printf("The value of integer is %d\n",     myUnion.integer);
  32.    printf("The value of floating is %f\n\n",  myUnion.floating);
  33.    printf("The value of \"cast\" is %f\n",    (float) myUnion.integer);
  34.    printf("The value of \"magic\" is %f\n\n", myUnion.integer);
  35.    printf("The meaning of life ``%d''\n",     (int) myUnion.floating);
  36.    return (0);
  37. }
  38.  
  39.    The optput is :
  40.  
  41.       The value of integer is 1
  42.       The value of floating is 0.000000
  43.       The value of cast is 1.000000
  44.       The value of magic is 0.000000
  45.       The meaning of life ``0''
  46.  
  47.  
  48.     My question is : Why myUnion.integer is 1 but myUnion.floating is
  49.     0.000000;Why myUnion.integer change to 0.000000 in 4th printf(),
  50.     and why myUnion.floating is 0 in 5th printf()?
  51.  
  52.